package controllers;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import org.bson.types.ObjectId;
import org.h2.engine.DbObjectBase;
import org.junit.Before;
import org.junit.runner.Request;

import com.mongodb.BasicDBObject;

import controllers.api.API;
import controllers.api.Users;


import models.User;
import models.Group;
import models.WallComment;
import models.WallThread;
import models.myThread;
import play.mvc.Controller;
import play.mvc.Scope.Params;

import play.modules.mongo.MongoCursor;
import play.modules.mongo.MongoDB;
import play.modules.mongo.MongoModel;
public class UserProfile extends API{
	
	private static Boolean isLoggedIn(){
		return session.contains("sessionUser");
	}
	private static User getSessionUser(){
		if(isLoggedIn()){
			String uid = session.get("sessionUser");
			return User.find("by_id",uid).first(); 
		}
		else return null;
	}
	
	@Before
	static void init(){
		if(!isLoggedIn()){
			redirect("/");
		}
	}
	/*public static void index(){
		if(isLoggedIn()){
			redirect("/profile/"+getSessionUser().getId());
		}"/profile/"
    	render();
    }*/
	
	public static void myprofile(){
		init();
		profile(getSessionUser().getId());
	}
	
	public static void profile(String id){
		init();
		Map<String, Object> args = new HashMap<String, Object>();
		User pUser = User.find("by_id",id).first();
		args.put("user", pUser);
		args.put("sessionUser",getSessionUser().getId());
		args.put("isMe", getSessionUser().getId().equals(pUser.getId()));
		Group group=Group.find("by_id", pUser.getGroupID()).first();
		List<User> members=User.find("byGroupid",pUser.getGroupID()).fetch();	
		List<WallThread> wallpost=WallThread.find("byFromId",pUser.getId()).fetch();
		
		args.put("wallpost",wallpost);
	    args.put("members",members);
	    args.put("photos",pUser.getPhoto());
	    	renderTemplate(args);
	}

	public static void group(){
		init();
		Map<String,Object> args = new HashMap<String,Object>();
		Group group=Group.find("by_id",getSessionUser().getGroupID()).first();
		args.put("sessionUser",getSessionUser().getId());
		args.put("group", group);
		List<User> members=User.find("byGroupid",getSessionUser().getGroupID()).fetch();
		args.put("members",members);
		renderTemplate(args);	
	}
	
	public static void settings(){
		init();
		Map<String,Object> args = new HashMap<String,Object>();
		args.put("sessionUser",getSessionUser().getId());
		renderTemplate(args);
		
	}
	public static void myphoto(){
		photo(getSessionUser().getId());
	}
	public static void photo(String id){
		Map<String, Object> args = new HashMap<String, Object>();
		User pUser = User.find("by_id",id).first();
    	args.put("sessionUser",getSessionUser().getId());
    	args.put("isMe", getSessionUser().getId().equals(pUser.getId()));
    	args.put("photos",pUser.getPhoto());
		renderTemplate(args);
}
	public static void mymembers(){
		init();
		members(getSessionUser().getId());
	}
	public static void members(String id){
		init();
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("sessionUser",getSessionUser().getId());
		User pUser = User.find("by_id",id).first();
		List<User> members=User.find("byGroupid",pUser.getGroupID()).fetch();
	    args.put("members",members);
		    	renderTemplate(args);
	}
	/*public static void dialogs(){
		init();
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("sessionUser",getSessionUser().getId());
    	renderTemplate(args);
	}*/
	public static void inbox(){
		init();
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("sessionUser",getSessionUser().getId());
		List<MongoModel> mythread=myThread.find("byToId",getSessionUser().getId()).fetch();
		args.put("mythread",mythread);
    	renderTemplate(args);
	}
	public static void outbox(){
		init();
		Map<String, Object> args = new HashMap<String, Object>();
		args.put("sessionUser",getSessionUser().getId());
		List<myThread> mythread=myThread.find("byFromId",getSessionUser().getId()).fetch();
		args.put("mythread",mythread);
    	renderTemplate(args);
	}

	public static void logout(){
		session.clear();
		redirect("/");
	}	
}
 	

